/*******************************************************************************
 * @brief: systask
 * @Author: aple
 * @Date: 2022-12-04 10:37:03
 * @LastEditors: aple
 * @LastEditTime: 2022-12-07 19:27:41
 * @Verision: 1.0.0.0
 ******************************************************************************/
#include "bsp_api.h"
#include "pincfg.h"
#include "bsp_adc.h"
#include "sys_task.h"

#include "framework.h"
#include "mytype.h"
#include "filtern.h"
#include "lib_surport.h"
#include "crc_ext.h"
#include "swap.h"
#include "bsp_ui_spi.h"
#include "ui_touch.h"

/*****************************************************************************
 * @brief   order
 *****************************************************************************/
obj_init_sys(system_info);
obj_init_sys(task_init);
obj_app_order(light_manage);
obj_app_order(check_handle);
obj_app_order(adc_resault_handle);

/*****************************************************************************
 * @brief   var
 *****************************************************************************/
sys_run_st_type Gst;
ui_text_in_obj_type uin;

/*****************************************************************************
 * @brief   fun
 *****************************************************************************/
void task_run_arg_init(void);
void err_handle(void);

op_cl_exe_type opcl;

/*****************************************************************************
 * @brief   system task.
 *****************************************************************************/
void task_system(const void *argument)
{
	dbg_puts("start\n");

	thread_create();

	sys_app_init();

	app_tick_resume();

	while (1)
	{
		sys_app_run_once();

		sc_pulse_creat(app_tick_get());

		Gst.hal.utick = app_tick_get();

		// rtc
		if (0 == Gst.hal.rtc_ok)
		{
			if (sc_pulse.pulse_1s)
			{
				sig_time_type stim;
				rtc_time_run_sec(&Gst.hal.sim_rtc, 1);
				Gst.hal.rtc = time_to_sig(&Gst.hal.sim_rtc);
			}
		}
		else
		{
			time_sig_to_time(Gst.hal.rtc, &Gst.hal.sim_rtc);
		}

		sub2_add8(10, 102);

		osSignalWait(0, ms_ticks(20));
	}
}

/*****************************************************************************
 * @brief   system init.
 *****************************************************************************/
void task_init(void)
{
	void *buff;

	opcl.timeout = 5000;
	opcl.exe_max = 3;
	opcl.ok_delay = 1000;
	opcl.out_delay = 3000;
}

/*******************************************************************************
 * @brief: adc resault handle
 * @param {*}
 * @return {*}
 ******************************************************************************/
void adc_resault_handle(void)
{
	UU32 sum;

	if (!adc_man.st_hal_end)
		return;
	adc_man.st_hal_end = 0;

	loop(ADC_CH_NUM)
	{
		sum = sum_u32(adc_man.list[index], array_len(adc_man.list[index]));
		adc_man.val[index] = sum / 800;
	}
}

/*******************************************************************************
 * @brief: speed filter
 * @param {*}
 * @return {*}
 ******************************************************************************/
void light_manage(void)
{
	static word_tt led = {1};
	static word_tt ledo2 = {5};

	static word_tt led_opcl;

	sequence_linkage_manage(&Gst.sout, Gst.hal.utick, 2000);

	// led0.out(Gst.sout.out1);
	// led1.out(Gst.sout.out2);

	if (sc_pulse.pulse_250ms)
	{
		led.uval = bit16_roll_shift_left(led.uval, 4, 1);

		led0.out(led.bits.b0);
		led1.out(led.bits.b1);
		led2.out(led.bits.b2);
	}

	if (sc_pulse.pulse_250ms)
	{
		ledo2.uval = bit16_roll_shift_left(ledo2.uval, 16, 1);
		// ui_spi_obj.tx_buff[0] = ledo2.uval;
		// ui_spi_obj.tx_buff[1] = ledo2.uval;
		// ui_spi_obj.tx_buff[2] = ledo2.uval;
	}

	//opcl.open_req = (ui_spi_obj.rx_buff[0] & 0x0080) ? 1 : 0;
	//opcl.close_req = (ui_spi_obj.rx_buff[0] & 0x0040) ? 1 : 0;
	opcl.open_st = (ui_spi_obj.rx_buff[0] & 0x0020) ? 1 : 0;
	opcl.close_st = (ui_spi_obj.rx_buff[0] & 0x8000) ? 1 : 0;

	op_cl_exe_handle(&opcl);

	led_opcl.bits.b0 = opcl.open_out;
	led_opcl.bits.b1 = opcl.close_out;

	led_opcl.bits.b4 = opcl.open_err;
	led_opcl.bits.b5 = opcl.close_err;

	ui_spi_obj.tx_buff[2] = led_opcl.uval;
}

/*******************************************************************************
 * @brief: check handle
 * @param {*}
 * @return {*}
 ******************************************************************************/
void check_handle(void)
{

}

/*******************************************************************************
 * @brief: check
 * @param argc 参数数量
 * @param argv 参数指针
 * @return 错误代码
 ******************************************************************************/
int lib_check(int argc, char *argv[])
{
	int cnt = 1;
	if (argc > 1)
		cnt = atoi(argv[1]);
	while (cnt--)
	{
		lib_chk_test();
		check_handle();
	}
	dbg_print("res type : %d\n", lib_chk_err_type());
	dbg_print("fault last : %d\n", lib_chk_get());

	dbg_print("AHB1EN : 0x%08X\n", CRM->ahben1);
	dbg_print("AHB2EN : 0x%08X\n", CRM->ahben2);
	dbg_print("AHB3EN : 0x%08X\n", CRM->ahben3);
	dbg_print("APB1EN : 0x%08X\n", CRM->apb1en);
	dbg_print("APB2EN : 0x%08X\n", CRM->apb2en);

	dbg_print("PACFGR : 0x%08X\n", GPIOA->cfgr);
	dbg_print("PBCFGR : 0x%08X\n", GPIOB->cfgr & 0xFFF0FFFF);
	dbg_print("PCCFGR : 0x%08X\n", GPIOC->cfgr);
	dbg_print("PDCFGR : 0x%08X\n", GPIOD->cfgr);

	dbg_print("CPU ID : 0x%08X\n", inpw(0xE0042000));

	return 0;
}
DBG_FUN_EXPORT(lib_check);

/*****************************************************************************
 * @brief   system info.
 *
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void system_info(void)
{
	crm_clocks_freq_type clk;

	crm_clocks_freq_get(&clk);

	dbg_puts(SYMBOL_LINE_EQ PTCR);

	dbg_puts(asc_pic_light);

	dbg_puts(SYMBOL_LINE_EQ PTCR);
	dbg_print("HCLK = %d MHZ, PCLK1 = %d MHZ, PCLK2 = %d MHZ\n",
			  clk.ahb_freq / 1000000,
			  clk.apb1_freq / 1000000,
			  clk.apb2_freq / 1000000);
	dbg_puts(SYMBOL_LINE_EQ PTCR);
}

/*****************************************************************************
 * @brief   fatfs time
 *
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
uint32_t get_fattime(void)
{
	sig_time_type tim;

	tim.val = Gst.hal.rtc.val;

	tim.time.year += 20;

	return tim.val;
}

obj_init_app(uin_init);
void uin_init(void)
{
	static char buff[24];

	ui_text_input_reset(&uin, (uu08 *)buff, sizeof(buff));
	ui_num_text_input_rang(&uin, 1, 0, 6000000);
	uin.mode = UI_TEXT_MODE_TIM3;
}

int uin_add(int argc, char *argv[])
{
	if (argc < 2)
		return -1;

	ui_text_input_add(&uin, argv[1][0]);

	dbg_print("txt: %s\n val=%d\n", uin.buff, uin.num);

	return 0;
}
DBG_FUN_EXPORT(uin_add);

int uin_del(int argc, char *argv[])
{
	ui_text_input_del(&uin);

	dbg_print("txt: %s\n val=%d\n", uin.buff, uin.num);

	return 0;
}
DBG_FUN_EXPORT(uin_del);


/*******************************************************************************
 * @brief: check
 * @param argc 参数数量
 * @param argv 参数指针
 * @return 错误代码
 ******************************************************************************/
int str_format(int argc, char *argv[])
{
	char buff[64];
	char s1[] = "s1.start";
	char s2[] = "s2.start";
	char s3[] = "s3.start";
	char s4[] = "s4.start";
	
	size_t dat[] = {4096, 'x', (uu32)&s1, (uu32)&s2, (uu32)&s3, (uu32)&s4};
	
	if(argc < 2)
		return  1;
	
	strx_format(buff, argv[1], dat, 0x21);
	
	dbg_puts(buff);
	dbg_puts("\n");
	
	return 0;
}
DBG_FUN_EXPORT(str_format);
